package demo1;

public class Test {
    //直接插入排序
    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (array[j] > temp) {
                    array[j+1] = array[j];
                } else {
                    //array[j+1] = temp;
                    break;
                }
            }
            array[j+1] = temp;
        }
    }
    //希尔排序
    public void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }
    //对每组的插入排序
    private void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = i-gap;
            for (; j >= 0; j = j-gap) {
                if (temp < array[j]) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = temp;
        }
    }
    //选择排序
    public void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = array[i];
            for (int j = i+1; j < array.length; j++) {
                if (minIndex > array[j]) {
                    minIndex = array[j];
                }
            }
            int temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
        }
    }
    //另一种选择排序，同时找出最大值和最小值
    public void selectSort2(int[] array) {
        //下标
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int j = left+1; j <= right; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
                if (array[maxIndex] < array[j]) {
                    maxIndex = j;
                }
            }
            swap(array,minIndex,left);
            if (left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }
    private void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    //大根堆
    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private void createBigHeap(int[] array) {
        for (int i=(array.length-1-1)/2; i >= 0 ; i--) {
            shiftDown(array, i,array.length);
        }
    }

    private void shiftDown(int[] array,int parent,int end) {
        int child = 2*parent+1;
        while (child < end) {
            if (child+1 < end && array[child+1] > array[child]) {
                child++;
            }
            if (array[child] > array[parent]) {
                int temp = array[parent];
                array[parent] = array[child];
                array[child] = temp;
                parent=child;
                child = 2*parent+1;
            } else {
                break;
            }
        }
    }
    //冒泡排序
    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flag = true;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flag = false;
                }
            }
            if (flag) {
                break;
            }
        }
    }
    //快速排序
    public void quickSort(int[] array) {

    }

    private void quick(int[] array, int begin, int end) {
        if(begin >= end) {
            return;
        }
        int left = begin;
        int right = end;
        int pivot = parttion(array,left,right);
        quick(array,begin,pivot-1);
        quick(array,pivot+1,end);
    }
    private int parttion(int[] array, int left, int right) {
        int start = left;
        int tmp = array[left];
         while (left < right) {
            while (array[right] >= tmp && left < right) {
                right--;
            }
            while (array[left] <= tmp && left < right) {
                 left++;
            }
            swap(array,left,right);
         }
         swap(array,start,right);
         return right;
    }
}
